<?php
/**
 * Created by PhpStorm.
 * User: dream
 * Date: 2017/10/23
 * Time: 20:49
 */
$database_config = file_get_contents(__DIR__.'/tools_config.json');
$database_config = json_decode($database_config,true);
$dsn = $database_config["dsn"];
if(!empty($database_config["parent_class_name"]))
define("PARENT_CLASS",$database_config["parent_class_name"]);
else
define("PARENT_CLASS","MY_Model");
$user_name = $database_config["user_name"];
$pass_word = $database_config["pass_word"];
$table_prefix = $database_config["table_prefix"];
$path_prefix = $database_config["path_prefix"];
$path_prefix = empty($path_prefix)?"/new":"/".$path_prefix;
$class_path = __DIR__.$path_prefix;
$add_start_flag = $database_config["add_start_flag"]?true:false;
$end_prefix = $database_config["end_prefix"];
try{
    $pdo = new PDO($dsn,$user_name,$pass_word);

    $pdo_stament = $pdo->query("show tables");
    $tables = $pdo_stament->fetchAll(PDO::FETCH_ASSOC);
    $table_names = [];
    foreach ($tables as $table)
    {

        $pdo_stament = $pdo->query("SHOW FIELDS FROM ".reset($table)) ;
        $fields_info_arr = $pdo_stament->fetchAll(PDO::FETCH_ASSOC);
        $fileds_names = array_column($fields_info_arr,"Field");
        $table_names[reset($table)] = $fileds_names;

    }




    //获取变量名=>类名(表名转驼峰式)
    $results = [];
    $table_keys = array_keys($table_names);
    foreach ($table_keys as $key=> $table_name)
    {
        $new_name = str_replace($table_prefix,"",$table_name);
        $var_name = strtolower($new_name);
        $new_name = str_replace("_"," ",$new_name);
        $new_name = ucwords($new_name);
        $new_name = str_replace(" ","",$new_name);
        $results[$var_name] = $new_name;
    }

    //改变table_names的key变为驼峰
    foreach ($table_names as $key=>$fields)
    {
        $new_name = str_replace($table_prefix,"",$key);
        $var_name = strtolower($new_name);
        $new_name = str_replace("_"," ",$new_name);
        $new_name = ucwords($new_name);
        $new_name = str_replace(" ","",$new_name);
         unset($table_names[$key]);
         $table_names[$new_name] = $fields;
    }

    //生成指定目录
    if(!file_exists($class_path))
    {
        mkdir($class_path);
    }

    $results_str = $results;
    foreach ($results_str as $key=> $str)
    {
        $path_prefix2 = $database_config["path_prefix"].'/';
        if(empty($end_prefix))
        {
            $str = $path_prefix2.$str."_model";
        }else{
            $str = $path_prefix2.$str.$end_prefix;
        }
        $results_str[$key] = $str;
    }

    //附加信息到model的json配置文件中
    file_put_contents($class_path."/{$table_prefix}_mapper.json",json_encode($results_str,JSON_UNESCAPED_SLASHES));

    //生成class文件
    $annotations = "";
    foreach ($results as $table_name=>$class_name)
    {
        if(empty($end_prefix))
        {
            class_template($class_path,$class_name,$table_name,$table_names[$class_name]);
            $annotations .= annotation($class_name."_model",$table_name,$add_start_flag);
        }
        else{
            class_template($class_path,$class_name,$table_name,$end_prefix);
            $annotations .= annotation($class_name,$table_name.$end_prefix,$add_start_flag);
        }

    }

    //生成Ioc代码提示注释

    file_put_contents($class_path."$path_prefix".'_'."annotation.txt",$annotations);
}catch(Exception $e)
{
    echo "错误信息:".$e->getMessage();
}


//生成Ioc代码提示注释

function annotation($class_name,$var_name,$start_flag=true)
{
    if($start_flag)
      $annotation = "*@property $class_name  \$$var_name\n";
    else
      $annotation = "@property $class_name  \$$var_name\n";
      return $annotation;
}

//输出  php文件
function class_template($path,$class_name,$table_name,$fields=[],$add_end_prefix="_model",$parent_class=PARENT_CLASS)
{
    $class_name .= $add_end_prefix;

    $class_template_str = "<?php ".PHP_EOL;
    $field_str = "/*".PHP_EOL;
    foreach ($fields as $field)
    {
        $field_str.= "* @property $".$field.PHP_EOL;
    }
    $class_template_str.=$field_str;

    $class_template_str.="*/";
    $class_template_str .=
        "
class {$class_name} extends {$parent_class} 
{      
   protected \$table = \"{$table_name}\";
        
   public function __construct()
   {
       parent::__construct();
   }
} ";
    file_put_contents($path.'/'.$class_name.'.php',$class_template_str);
    return true;
}





